package homework;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

public class TankClient extends Frame {
    public static final int GAME_WIDTH = 800;
    public static final int GAME_HEIGHT = 600;
    Tank myTank = new Tank(50, 50, true,this);
    Tank enemyTank = new Tank(300, 100, false, this);
    Wall wall1 = new Wall(480, 150, 20, 150,this);
    Wall wall2 = new Wall(70, 500, 200, 10,this);
    Blood b =new Blood();
    List<Missile> missiles = new ArrayList<Missile>();
    List<Explode> explodes=new ArrayList<Explode>();
    List<Tank> tanks = new ArrayList<Tank>();
    Image offScreenImage = null;
    public void paint(Graphics g){
        g.drawString("missiles count: " + missiles.size(), 10, 50);
        g.drawString("explodes count: "+explodes.size(),10,70);
        g.drawString("tanks count: " + tanks.size(), 10, 90);
        g.drawString("mytanks blood: "+myTank.getLife(),10,110);
        if (tanks.size() <= 0) {
            for (int i = 0; i < 8; i++) {
                tanks.add(new Tank(50 + 40 * (i + 1), 50, false, this));
            }
        }
        for(int i = 0; i < missiles.size(); i++) {
            Missile m = missiles.get(i);
            m.hitTank(myTank);
            m.hitTank(enemyTank);
            m.hitTanks(tanks);
            m.hitWall(wall1);
            m.hitWall(wall2);
            m.draw(g);
            //if(!m.isLive()) missiles.remove(m);
            //else m.draw(g);
        }
        for (int i=0;i<explodes.size();i++){
            Explode e=explodes.get(i);
            e.draw(g);
        }
        myTank.collidesWithWall(wall2);
        myTank.collidesWithWall(wall1);
        myTank.collidesWithTanks(tanks);
        for (int i=0;i<tanks.size();i++){
            enemyTank=tanks.get(i);
            enemyTank.draw(g);
            enemyTank.collidesWithWall(wall1);
            enemyTank.collidesWithWall(wall2);
            enemyTank.collidesWithTanks(tanks);
        }
        wall1.draw(g);
        wall2.draw(g);
        b.draw(g);
        myTank.eat(b);
        myTank.draw(g);
    }
    public void update(Graphics g) {
        if(offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH,
                    GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.GREEN);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        print(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }
    public void launchFrame() {
        for(int i = 0; i < 10; i++) {
            tanks.add(new Tank(50 + 40 * (i + 1), 50, false, this));
        }
        this.setLocation(300, 50);
        this.setSize(GAME_WIDTH, GAME_HEIGHT);
        this.setTitle("TankWar");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.setResizable(false);
        this.setBackground(Color.GREEN);
        this.addKeyListener(new KeyMonitor());
        setVisible(true);
        new Thread(new PaintThread()).start();
    }

    public static void main(String[] args) {
        TankClient tc = new TankClient();
        tc.launchFrame();
    }
    private class PaintThread implements Runnable{
        @Override
        public void run() {
            while (true){
                repaint();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private class KeyMonitor extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            myTank.KeyPressed(e);
        }
        public void keyReleased(KeyEvent e) {
            myTank.KeyReleased(e);
        }
    }
}